package com.cskaoyan.service.promotion;

import com.cskaoyan.bean.BaseParam;

import com.cskaoyan.bean.promotion.*;


import com.cskaoyan.mapper.PosterMapper;
import com.cskaoyan.util.DateUtils;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 推广管理控制层
 * @Author: WD created on 2021-09-15 17:43
 * @Version:
 */
@Service
public class PromotionServiceImpl implements PromotionService {

    @Autowired
    PosterMapper posterMapper;


    @Override
    public ShowPosterListVO getPosters(BaseParam param, String name, String content) {
        PageUtil.start(param);
        PosterExample posterExample = new PosterExample();
        posterExample.setOrderByClause(param.getSort() + " " + param.getOrder());

        PosterExample.Criteria criteria = posterExample.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtil.isEmpty(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        //逻辑删除的
        criteria.andDeletedEqualTo(false);
        List<Poster> posters = posterMapper.selectByExample(posterExample);
        PageInfo<Poster> posterPageInfo = new PageInfo<>(posters);
        ArrayList<PosterListVO> posterListVOs = new ArrayList<>();
        for (Poster poster : posters) {
            posterListVOs.add(new PosterListVO(poster));
        }
        ShowPosterListVO showPosterListVO = new ShowPosterListVO(posterPageInfo, posterListVOs);

        return showPosterListVO;

    }



    @Override
    public CreatePosterVO createPoster(CreatePosterBO promotion) {
        Poster poster = new Poster(promotion);

        posterMapper.creatPoster(poster);

        Integer id = poster.getId();

        CreatePosterVO posterVO = new CreatePosterVO(poster);

        return posterVO;

    }

    @Override
    public UpdatePoster updatePoster(UpdatePoster poster) {
        Poster poster1 = new Poster(poster);
        poster1.setUpdateTime(new Date());
        poster.setUpdateTime(DateUtils.dateFormat(new Date()));
        posterMapper.updateByPrimaryKey(poster1);
        return poster;

    }

    @Override
    public Integer deletePosterNotReal(DeletePosterBO posterBO) {
        Poster poster = new Poster(posterBO);

        int i = posterMapper.updateByPrimaryKey(poster);
        return i;
    }
}
